﻿using LKVMLMADMIN.Models;
using LKVMLMLib;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LKVMLMADMIN.Controllers
{
    public class CauHinhController : Controller
    {        
        public ActionResult CauHinhChuKy()
        {
            if (Session["User"] != null)
            {
                var extenssion = new LKVService.User();
                var ListCHuKy = extenssion.GetListChuKy();
                ViewBag.ListChuKy = ListCHuKy;
                return View();
            }
            return RedirectToAction("Login", "Account");
        }
        public ActionResult AddChuky()
        {
            if (Session["User"] != null)
            {
            return View();
            }
            return RedirectToAction("Login", "Account");
        }
        [HttpPost]
        public ActionResult AddChuky(string TenChuKy = "", int From = 0, int To=0)
        {
            if(TenChuKy!="" && From!=0 && To!=0)
            { 
                var extenssion = new LKVService.User();
                var Newchuky = new ChuKyConfig();
                Newchuky.TenChuKy = TenChuKy;
                Newchuky.From = From;
                Newchuky.To = To;
                extenssion.AddChuKyConfig(Newchuky);
                return RedirectToAction("CauHinhChuKy", "CauHinh");
            }
            else
            return View();
            
        }
        public ActionResult EditChuky(ChuKyModel model, int ckid=0)
        {
            var extension = new LKVService.User();
            var chukychon = extension.ChooseChuKyConfig(ckid);
            model.TenChuKy = chukychon.TenChuKy;
            model.From = chukychon.From;
            model.To = chukychon.To;
            model.ChuKyID = chukychon.ChuKyID;
            return View(model);
        }
        [HttpPost]
        public ActionResult EditChuky(ChuKyModel model)
        {
            var extenssion = new LKVService.User();
            if (model.ChuKyID != 0)
            {
                var ChuKyCHon = extenssion.ChooseChuKyConfig(model.ChuKyID);
                ChuKyCHon.TenChuKy = model.TenChuKy;
                ChuKyCHon.From = model.From;
                ChuKyCHon.To = model.To;
                extenssion.UpdateChykyconfig(ChuKyCHon);
                return RedirectToAction("CauHinhChuKy", "CauHinh");
            }
            else
                return View();
        }
        public ActionResult Removechuky(int id)
        {
            var extenssion = new LKVService.User();
            extenssion.RemoveChukyConfig(id);
            return RedirectToAction("CauHinhChuKy", "CauHinh");
        }
        public ActionResult Exchange(ExchangeModel model)
        {
            var extenssion = new LKVService.ProfitService();
            var In = extenssion.getListExchange().Where(w => w.Name == "IN").Select(s => s.Value).SingleOrDefault();
            model.DauVao =Convert.ToDouble( In);
            var Out = extenssion.getListExchange().Where(w => w.Name == "OUT").Select(s => s.Value).SingleOrDefault();
            model.DauRa = Out;
            return View(model);
        }
        [HttpPost]
        public ActionResult Exchange(ExchangeModel model , string a ="")
        {
            var extenssion = new LKVService.ProfitService();
            var In = extenssion.getListExchange().Where(w => w.Name == "IN").SingleOrDefault();
            In.Value = Convert.ToDouble(model.DauVao);
            var Out = extenssion.getListExchange().Where(w => w.Name == "OUT").SingleOrDefault();
            Out.Value = Convert.ToDouble(model.DauRa);
            extenssion.UpdateExchange(In);
            extenssion.UpdateExchange(Out);
            return View();
        }
        public ActionResult ConfigUser()
        {
            if (Session["User"] != null)
            {
                // hoang viet
                var extenssion = new LKVService.User();
                var ListUserLevel = extenssion.getUserLevels().ToList();
                if (ListUserLevel.Count > 0)
                    ViewBag.ListUSLV = ListUserLevel;
                return View();
            }
            return RedirectToAction("Login", "Account");                     
        }
        public ActionResult DeleteUserLevel( int idDL =0)
        {
            if(idDL!=0)
            {
                var extension = new LKVService.User();
                extension.RemoveUserLevel(idDL);
            }
            return RedirectToAction("ConfigUser", "CauHinh");
        }
        public ActionResult  AddUserLevel()
        {
            return View();
        }
        [HttpPost]
        public ActionResult AddUserLevel(FormCollection fc, string Name="" ,double Amount =0, double Maximum =0, double Minimum=0)
        {
            if (Name != "" && Amount != 0 && Maximum != 0 && Minimum != 0)
            {
                var ad = fc["Amount"];
                if (ad.Contains(','))
                    ad = ad.Replace(',', '.');
                double _Amount = double.Parse(ad, CultureInfo.InvariantCulture);
                var cd = fc["Maximum"];
                if (cd.Contains(','))
                    cd = cd.Replace(',', '.');
                double _Maximum = double.Parse(cd, CultureInfo.InvariantCulture);
                var ef = fc["Maximum"];
                if (ef.Contains(','))
                    ef = ef.Replace(',', '.');
                double _Minimum = double.Parse(ef, CultureInfo.InvariantCulture);
                var extenssion = new LKVService.User();
                var c = new LKVMLMLib.UserLevel();
                c.Amount = _Amount;
                c.Maximum = _Maximum;
                c.Minimum = _Minimum;
                c.Name = Name;
                extenssion.AddUserLevel(c);
                return RedirectToAction("ConfigUser", "CauHinh");
            }
            else
                return View();
        }
        public ActionResult UpdateUserLevel(UserLevelModels model, int idED = 0)
        {
            var extension = new LKVService.User();
            var ChooseUSLV = extension.getUserLevelById(idED);
            if(ChooseUSLV!=null)
            {
              
            model.Id = ChooseUSLV.Id;
            model.Name = ChooseUSLV.Name;
            model.Maximum = ChooseUSLV.Maximum;
            model.Minimum = ChooseUSLV.Minimum;
            model.Amount = ChooseUSLV.Amount;
            }
            return View(model);
        }
        [HttpPost]
        public ActionResult UpdateUserLevel(UserLevelModels model, FormCollection fc)
        {
            var extension = new LKVService.User();
            var ChooseUSLV = extension.getUserLevelById(model.Id);
            var ad = fc["Amount"];
            if (ad.Contains(','))
                ad = ad.Replace(',', '.');
            double _Amount = double.Parse(ad, CultureInfo.InvariantCulture);
            var cd = fc["Maximum"];
            if (cd.Contains(','))
                cd = cd.Replace(',', '.');
            double _Maximum = double.Parse(cd, CultureInfo.InvariantCulture);
            var gh = fc["Minimum"];
            if (gh.Contains(','))
                gh = gh.Replace(',', '.');
            double _Minimum = double.Parse(gh, CultureInfo.InvariantCulture);
            ChooseUSLV.Name = model.Name;
            ChooseUSLV.Maximum = _Maximum;
            ChooseUSLV.Minimum = _Minimum;
            ChooseUSLV.Amount = _Amount;
            extension.UpdateUserLevel(ChooseUSLV);
            return RedirectToAction("ConfigUser", "CauHinh");
        }
        public ActionResult ConfigFee( FeeConfigModels model, int Id=0)
        {
            if (Session["User"] != null)
            {
                var extenssion = new LKVService.User();
                var chooseFee = extenssion.getFeeConfig();
                model.Addfund = chooseFee.Addfund;
                model.SellStock = chooseFee.SellStock;
                model.Id = chooseFee.Id;
                model.BuyStock = chooseFee.BuyStock;
                model.Cashout = chooseFee.Cashout;
                model.PremiumEdit = chooseFee.PremiumEdit;
                return View(model);
            }
            return RedirectToAction("Login", "Account");
         
        }
        [HttpPost]
        public ActionResult ConfigFee(FeeConfigModels model, FormCollection fc)
        {
            if (Session["User"] != null)
            {
                var ad = fc["Addfund"];
                if (ad.Contains(','))
                    ad = ad.Replace(',', '.');
                double _Addfund = double.Parse(ad, CultureInfo.InvariantCulture);
                var cd = fc["SellStock"];
                if (cd.Contains(','))
                    cd = cd.Replace(',', '.');
                double _SellStock = double.Parse(cd, CultureInfo.InvariantCulture);
                var ef = fc["BuyStock"];
                if (ef.Contains(','))
                    ef = ef.Replace(',', '.');
                double _BuyStock = double.Parse(ef, CultureInfo.InvariantCulture);
                var gh = fc["Cashout"];
                if (gh.Contains(','))
                    gh = gh.Replace(',', '.');
                double _Cashout = double.Parse(gh, CultureInfo.InvariantCulture);
                var ik = fc["PremiumEdit"];
                if (ik.Contains(','))
                    ik = cd.Replace(',', '.');
                double _PremiumEdit = double.Parse(ik, CultureInfo.InvariantCulture);
                var extenssion = new LKVService.User();
                var chooseFee = extenssion.getFeeConfig();
                chooseFee.Addfund = _Addfund;
                chooseFee.SellStock = _SellStock;
                chooseFee.BuyStock = _BuyStock;
                chooseFee.Cashout = _Cashout;
                chooseFee.PremiumEdit = _PremiumEdit;
                extenssion.UpdateFeeConfig(chooseFee);
                return RedirectToAction("ListFeeConfig", "CauHinh");
            }
            return RedirectToAction("Login", "Account");

        }
        public ActionResult ListFeeConfig()
        {
            if (Session["User"] != null)
            {
                var extension = new LKVService.User();
                var chooseFee = extension.ListFeeConfig();
                ViewBag.ListFee = chooseFee;
                return View();
            }
            return RedirectToAction("Login", "Account");
        }
        public ActionResult ConFigQuy()
        {
            var extension = new LKVService.User();
            var ListCaskBankConfig = extension.LoadListCaskBankConfig();           
            ViewBag.ListCHQ = ListCaskBankConfig;
            return View();
        }
        public ActionResult CauHinhHeThong()
        {           
            if (Session["User"] != null)
            {
                var extenssion = new LKVService.OrderService();
                var ListGconfig = extenssion.GetListGconfig();
                ViewBag.ListGconfig = ListGconfig;
                return View();
            }
            return RedirectToAction("Login", "Account");
        }
        public ActionResult EditGConfig(GconfigModels model, int ided =0)
        {                         
                var extenssion = new LKVService.OrderService();
                var ListMain = new List<bool?>();              
                var ListGconfig = extenssion.GetGConfig();
                model.ROI = ListGconfig.ROI;
                model.isMaintainent = ListGconfig.isMaintainent;
                model.PenddingWallet = ListGconfig.PenddingWallet;
                ListMain.Add(true);
                ListMain.Add(false);
                var List = extenssion.GetListGconfig();
                ViewBag.CheckMain = ListMain.Select(s => new SelectListItem
                {
                    Text = s==true? "Cho phép":"Chưa cho phép",
                    Value = s.ToString(),
                    Selected =Convert.ToBoolean( ListGconfig.isMaintainent)
                });
                ViewBag.CheckPendding = ListMain.Select(s => new SelectListItem
                {
                    Text = s == true ? "Cho phép" : "Chưa cho phép",
                    Value = s.ToString(),
                    Selected = Convert.ToBoolean(ListGconfig.PenddingWallet)
                }); 
            return View(model);
        }
        [HttpPost]
        public ActionResult EditGConfig(GconfigModels model, FormCollection fc)
        {
            var extenssion = new LKVService.OrderService();         
            var Gconfig = extenssion.GetGConfig();
            var ad = fc["ROI"];
            if (ad.Contains(','))
                ad = ad.Replace(',', '.');
            double _ROI = double.Parse(ad, CultureInfo.InvariantCulture);
            Gconfig.ROI = _ROI;
            Gconfig.isMaintainent = model.isMaintainent;
            Gconfig.PenddingWallet = model.PenddingWallet;
            extenssion.UpdateGconfig(Gconfig);
            return RedirectToAction("CauHinhHeThong", "CauHinh");  
        }
        public ActionResult DeleteCaskBankConfig( int idDL=0)
        {
            if(idDL!=0)
            {
                var extenssion = new LKVService.User();
                extenssion.RemoveCaskBankConfig(idDL);
            }
            return RedirectToAction("ConFigQuy", "CauHinh");
        }
        public ActionResult AddCaskBankConfig()
        {
            var db = new LKVService.User();
            var ListUser = db.getListUser().Select(s => new { value = s.UserId, text = s.UserName }).ToList();
            ListUser.Insert(0, new { value = 0, text = "Chọn Người dùng" });
            ViewBag.username = ListUser;
            return View();
        }
        [HttpPost]
        public ActionResult AddCaskBankConfig(FormCollection fc, int UserId =0, string Type ="", double Percent =0)
        {
            if(UserId!=0 && Type!="" && Percent!=0)
            {
                var ad = fc["Percent"];
                if (ad.Contains(','))
                    ad = ad.Replace(',', '.');
                double _Percent = double.Parse(ad, CultureInfo.InvariantCulture);
                var extenssion = new LKVService.User();
                var _check = extenssion.GetCaskBankConfig(UserId);
                if(_check==null)
                {
                    var CB = new LKVMLMLib.CashBankConfig();
                    CB.UserId = UserId;
                    CB.Type = Type;
                    CB.Percent = _Percent;
                    extenssion.AddCaskBankConfig(CB);
                }
            }
            return RedirectToAction("ConFigQuy", "CauHinh");
        }
        public ActionResult UpdateCaskbankConfig( int UserId =0)
        {
            var extenssion = new LKVService.User();
           // var chooseCaskbankConfig = extenssion.GetCaskBankConfig(UserId);
            var ListUser = extenssion.getListUser().Select(s => new { value = s.UserId, text = s.UserName }).ToList();
            ListUser.Insert(0, new { value = 0, text = "Chọn Người dùng" });
            ViewBag.username = ListUser;
            var ListUSType = extenssion.GetCaskBankConfig(UserId);
            if(ListUSType!=null)
            {
                ViewBag.Percent = ListUSType.Percent;
                ViewBag.type = ListUSType.Type;
            }       
            var chooseUser =extenssion.findUserById(UserId);
            if (chooseUser != null)
                ViewBag.UserID = chooseUser.UserId;
          
            return View();
        }
        [HttpPost]
        public ActionResult UpdateCaskbankConfig(FormCollection fc, int UserId = 0, string Type = "", double Percent = 0)
        {
            var extenssion = new LKVService.User();
            var chooseCaskbank = extenssion.GetCaskBankConfig(UserId);
            if(chooseCaskbank!=null)
            {
                var ad = fc["Percent"];
                if (ad.Contains(','))
                    ad = ad.Replace(',', '.');
                double _Percent = double.Parse(ad, CultureInfo.InvariantCulture);
                chooseCaskbank.Type = Type;
                chooseCaskbank.Percent = _Percent;
                extenssion.UpdateCaskBankConfig(chooseCaskbank);
            }
            return RedirectToAction("ConFigQuy", "CauHinh");
        }
    }
}
